package org.supda.system.service.impl;

import java.util.List;

import javax.transaction.Transactional;

import net.sf.ehcache.util.FindBugsSuppressWarnings;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.supda.system.dao.TServerWarnPropertiesDao;
import org.supda.system.entity.TRole;
import org.supda.system.entity.TServerWarnProperties;
import org.supda.system.entity.TUser;
import org.supda.system.service.RoleService;
import org.supda.system.service.TServerWarnPropertiesService;
import org.supda.system.util.DataTablePageUtil;
import org.supda.system.util.EntityUpdataUtils;

@Service("TServerWarnPropertiesService")
@Transactional
public class TServerWarnPropertiesServiceImpl implements TServerWarnPropertiesService{

	@Autowired
	private TServerWarnPropertiesDao serverWarnPropertiesDao;
	@Autowired
	private RoleService roleService;
	
	@Override
	public void listByDataTable(DataTablePageUtil<TServerWarnProperties> dtp) {
		serverWarnPropertiesDao.listByDataTable(dtp, TServerWarnProperties.class);
	}

	@Override
	public TServerWarnProperties show(Long id) {
		
		return null;
	}

	@Override
	public TServerWarnProperties getLastWarnProperties() {
		
		List<TServerWarnProperties> tswps = serverWarnPropertiesDao.find("FROM TServerWarnProperties ORDER BY createTime DESC");
		
		if(tswps!=null && tswps.size()>0)
			return tswps.get(0);
		
		return null;
	}

	@Override
	public void edit(TServerWarnProperties properties, Long propertitesId) {
		
		TServerWarnProperties twp = findById(propertitesId);
		
		if(twp == null)
			return ; 
		
		
		TServerWarnProperties serverWarnProperties = 
				EntityUpdataUtils.update(TServerWarnProperties.class, twp , properties);
		
		if(properties.getTRole()!=null){
			TRole role = roleService.findByRoleId(properties.getTRole().getId());
			if(role!= null)
				serverWarnProperties.setTRole(role);
		}

		serverWarnPropertiesDao.update(serverWarnProperties);
	}

	@Override
	public TServerWarnProperties findById(Long id) {
		return serverWarnPropertiesDao.get(TServerWarnProperties.class, id);
	}

}
